package cn.bocweb.spinning.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ShopListBean;

/**
 * Created by chenchangxing on 15/12/22.
 */
public class IntegralChargeAdapter extends RecyclerView.Adapter {

    List<ShopListBean.DataBean> mDataBeanList;
    OnItemClickListener onItemClickListener;

    public IntegralChargeAdapter(List<ShopListBean.DataBean> mDataBeanList) {
        this.mDataBeanList = mDataBeanList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_integral_charge, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        ViewHolder myHolder = (ViewHolder) holder;

        Glide
                .with(holder.itemView.getContext())
                .load(mDataBeanList.get(position).getPhoto())
//                .placeholder()
                .into(myHolder.mIcon);
        myHolder.mProductName.setText(mDataBeanList.get(position).getTitle());
        myHolder.mIntegral.setText(mDataBeanList.get(position).getScore());
        myHolder.mLeft.setText(mDataBeanList.get(position).getStock());
        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(
                        holder.getAdapterPosition(),
                        mDataBeanList.get(holder.getAdapterPosition()).getUrl(),
                        mDataBeanList.get(holder.getAdapterPosition()).getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataBeanList.size();
    }


    public interface OnItemClickListener {
        void onClick(int position, String url, String id);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.icon)
        ImageView mIcon;
        @Bind(R.id.product_name)
        TextView mProductName;
        @Bind(R.id.integral)
        TextView mIntegral;
        @Bind(R.id.left)
        TextView mLeft;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
